home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Games / dynAMIte / Developer / C / dynbot.c < prev   
C/C++ Source or Header  |  2001-06-24  |  2KB  |  118 lines

  1. #include    <stdlib.h>
  2. #include     <stdio.h>
  3. #include    <time.h>
  4.  
  5. #include     <exec/semaphores.h>
  6. #include     <dos/dos.h>
  7. #include     <proto/exec.h>
  8. #include     <proto/dos.h>
  9.  
  10. #include     "dynamite.h"
  11.  
  12.  
  13. int
  14. main()
  15. {
  16.     struct dynamitesemaphore    *dynasema;
  17.     struct player                 *ourplayer;
  18.     BOOL                        done = FALSE,
  19.                                 delay;
  20.  
  21.     /* check if dynamite is running */
  22.  
  23.     srand (time(NULL));
  24.  
  25.     Forbid();    /* try to find the semaphore */
  26.     dynasema=(struct dynamitesemaphore *)FindSemaphore("dynAMIte.0");
  27.     Permit();
  28.  
  29.     if(dynasema)
  30.         {
  31.         printf("dynAMIte is started\n");
  32.  
  33.         ObtainSemaphore(&dynasema->sema);    /* lock it */
  34.  
  35.         /*
  36.         increase opencount to tell dynamite that you are using the
  37.         semaphore. dynamite will remove semaphore only if opencnt is
  38.         0 at its end
  39.         */
  40.  
  41.         dynasema->opencnt++;
  42.         ReleaseSemaphore(&dynasema->sema);    /* release it */
  43.  
  44.         printf("Clients using the semaphore: %ld\n",dynasema->opencnt);
  45.  
  46.         while (!done)
  47.           {
  48.             if(CheckSignal(SIGBREAKF_CTRL_C))
  49.                 {
  50.                 done=TRUE;
  51.                 }
  52.             else
  53.                 {
  54.                 delay=TRUE;
  55.                 ObtainSemaphore(&dynasema->sema);
  56.  
  57.                 if(dynasema->quit)
  58.                     {
  59.                     /* dynamite wants to quit, so we do dynamite a favour */
  60.                     printf("dynAMIte is about to quit...\n");
  61.                     done=TRUE;
  62.                     }
  63.                 else
  64.                     {
  65.                     /* if a game is running */
  66.                     if(dynasema->gamerunning>=GAME_GAME)
  67.                         {
  68.                         /* and player is no observer */
  69.                         if(dynasema->thisplayer<8)
  70.                             {
  71.                             delay=FALSE;
  72.  
  73.                             ourplayer=dynasema->players[dynasema->thisplayer];
  74.  
  75.                             /* and our player is alive */
  76.                             if(ourplayer->dead)
  77.                                 {
  78.                                 /* do your AI stuff */
  79.                                 dynasema->walk= rand()%DIR_UP+1;
  80.                                 }
  81.                             }
  82.                         }
  83.  
  84.                   }
  85.  
  86.                 ReleaseSemaphore(&dynasema->sema);
  87.  
  88.                 if(delay)
  89.                     {
  90.                     /* no game is running */
  91.                     /* do a small delay to let the cpu do other things :( */
  92.                     Delay(10);
  93.                     }
  94.  
  95.             }
  96.         }
  97.  
  98.         ObtainSemaphore(&dynasema->sema);
  99.  
  100.         /*
  101.         decrease opencount to tell dynamite that you no longer need
  102.        the semaphore.
  103.        dynamite will remove the semaphore only if opencnt is
  104.        0 at the end
  105.         */
  106.  
  107.         dynasema->opencnt--;
  108.         ReleaseSemaphore(&dynasema->sema);
  109.         }
  110.     else
  111.         {
  112.         printf("dynamite is not running\n");
  113.         }
  114.  
  115.  
  116.     return EXIT_SUCCESS;
  117. }
  118.